package com.xuanwu.ai.flow.utils;

import com.alibaba.fastjson.JSONObject;
import com.xuanwu.ai.flow.constant.GlobalConstant;
import com.xuanwu.ai.flow.dto.CommonContextDTO;
import com.xuanwu.ai.flow.dto.StepNodeDataDTO;
import org.apache.commons.lang3.ObjectUtils;

public class NodeDataUtils {


    /**
     * 设置节点数据
     *
     * @param customContext
     * @param nodeId
     * @param nodeName
     * @param aiDetectResult
     */
    public static void setStepNodeData(CommonContextDTO customContext, String nodeId, String nodeName, JSONObject aiDetectResult) {
        StepNodeDataDTO stepNodeData = new StepNodeDataDTO();
        stepNodeData.setNodeId(nodeId);
        stepNodeData.setNodeName(nodeName);
        stepNodeData.setStatus(aiDetectResult.getInteger("status"));
        if (aiDetectResult.getInteger("status").equals(GlobalConstant.CMP_EXECUTE_FAIL)) {
            stepNodeData.setErrorMsg(aiDetectResult.getJSONObject("data").getString("errorMsg"));
        } else
            stepNodeData.setResultData(aiDetectResult.getJSONObject("data"));

        if (ObjectUtils.isEmpty(customContext.getNodeJsonResult())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(nodeId, stepNodeData);
            customContext.setNodeJsonResult(jsonObject);
        } else {
            customContext.getNodeJsonResult().put(nodeId, stepNodeData);
        }
    }
}
